package com.gongpingjialoan.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.City;
import com.gongpingjialoan.bean.Province;

import java.util.List;

/**
 * Created by Administrator on 2016/5/10.
 */
public class CitySelectAdapter extends BaseExpandableListAdapter {
    Context context;
    String checkcity = "";

    String checkprovince = "";

    List<Province> list;

    public CitySelectAdapter(Context context, List<Province> list) {
        this.context = context;
        this.list = list;
    }


    public void setData(List<Province> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public City getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).getCitylist().get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (list.get(groupPosition).getCitylist() == null) {
            return 0;
        }
        return list.get(groupPosition).getCitylist().size();
    }


    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView,
                             ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_new_city_child, null);
        }
        TextView nameT = (TextView) convertView.findViewById(R.id.name);
        ImageView img = (ImageView) convertView.findViewById(R.id.check);

        final City city = (City) getChild(groupPosition, childPosition);

        String cityname = city.getName();

        boolean ischeck = city.ischeck();
        img.setImageResource(ischeck ? R.drawable.checked : R.drawable.uncheck);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                city.setIscheck(!city.ischeck());
                final List<City> citylist = list.get(groupPosition).getCitylist();

                //代表点的全省车源
                if (TextUtils.isEmpty(city.getLetter())) {
                    for (int i = 1; i < citylist.size(); i++) {
                        City city1 = citylist.get(i);
                        city1.setIscheck(city.ischeck());
                    }

                } else {
                    int checkedSize = 0;
                    for (int i = 1; i < citylist.size(); i++) {
                        City city1 = citylist.get(i);
                        if (city1.ischeck()) {
                            checkedSize = checkedSize + 1;
                        }
                    }

                    if (checkedSize == citylist.size() - 1) {
                        citylist.get(0).setIscheck(true);
                    } else {
                        citylist.get(0).setIscheck(false);
                    }
                }

                notifyDataSetChanged();
            }
        });

        nameT.setText(cityname);

        return convertView;
    }

    @Override
    public Province getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_new_city_group, null);
        }

        TextView nameT = (TextView) convertView.findViewById(R.id.name);
        String prov = getGroup(groupPosition).getName();
        nameT.setText(prov);
        //textView.setTextSize(16);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    public String getCheckedCity() {
        checkcity = "";
        checkprovince = "";

        for (int i = 0; i < list.size(); i++) {
            Province province = list.get(i);
            List<City> citylist = province.getCitylist();
            for (int j = 0; j < citylist.size(); j++) {

                City city = citylist.get(j);
                if (j == 0) {
                    if (city.ischeck()) {

                        if (citylist.size() > 2) {
                            if (TextUtils.isEmpty(checkprovince)) {
                                checkprovince = province.getName();
                            } else {
                                checkprovince = checkprovince + "," + province.getName();
                            }
                        } else {
                            addAllCity(citylist);
                        }

                        break;
                    }
                } else {
                    if (city.ischeck()) {
                        if (TextUtils.isEmpty(checkcity)) {
                            checkcity = checkcity + city.getName();
                        } else {
                            checkcity = checkcity + "," + city.getName();
                        }
//                        list.add(city.getName());
                    }
                }
            }
        }


        return checkcity;

    }


    public String getCheckedProvice() {


        return checkprovince;

    }

    private void addAllCity(List<City> list) {
        for (int j = 1; j < list.size(); j++) {
            City city = list.get(j);
            if (TextUtils.isEmpty(checkcity)) {
                checkcity = checkcity + city.getName();
            } else {
                checkcity = checkcity + "," + city.getName();
            }
        }

    }

    public void clearCheck() {
        for (int i = 0; i < list.size(); i++) {
            Province province = list.get(i);
            List<City> citylist = province.getCitylist();
            for (int j = 0; j < citylist.size(); j++) {

                City city = citylist.get(j);
                if (city.ischeck()) {
                    city.setIscheck(false);
                }
            }
        }


    }

}
